"""
case type   : 资源池化
case name   : ss_ock_log_path参数验证
Create At   : 2023.10
Owner       : wx1224102
description :
    1、查看参数，校验原始默认值，当前参数值，参数类型，取值类型及范围
    2、修改参数为其他合理值，重启数据库使参数生效
    3、恢复原始值，恢复集群状态
expect      :
    1、原始值为空，当前参数值为空，参数类型为postmaster，取值类型为字符串
    2、修改成功，重启失败
    3、恢复原始值成功，集群状态正常
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.usernode = Node('PrimaryDbUser')
        self.param = 'ss_ock_log_path'
        self.ock_log_path = os.path.join(macro.DB_INSTANCE_PATH, 'ock_log')
        self.src = self.com.show_param(self.param)

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_shared_storage(self):
        text = '-----step1:查看参数，校验默认值等; expect:启动默认值为空,当前参数值为空,' \
               '参数类型为postmaster类型,取值类型为字符串类型-----'
        self.log.info(text)

        pri_config = f'''select name,setting,context,boot_val,vartype 
            from pg_settings where name = '{self.param}';'''
        self.log.info(pri_config)
        pri_res = self.pri_sh.execut_db_sql(pri_config)
        self.log.info(pri_res)

        res1 = pri_res.splitlines()[0].split('|')
        res2 = pri_res.splitlines()[-2].split('|')
        result = dict(zip(res1, res2))
        self.log.info(result)

        value_list1 = []
        for key, values in result.items():
            value_list1.append(values.strip())
        self.log.info(value_list1)
        self.assertEqual(f'{self.param}', value_list1[0].strip(), '参数名校验失败')
        self.assertEqual('', value_list1[1], '当前值校验失败')
        self.assertEqual('postmaster', value_list1[2].strip(), '参数类型校验失败')
        self.assertEqual('', value_list1[3], '默认值校验失败')
        self.assertEqual('string', value_list1[4].strip(), '取值类型校验失败')

        text = '-----step2:修改参数为其他值，重启集群; expect:修改成功，重启数据库失败-----'
        self.log.info(text)

        set_param1 = self.pri_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.param} = '{self.ock_log_path}'")
        self.log.info(set_param1)
        self.assertTrue(set_param1, '参数修改失败')

        db_restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start', '-t 60')
        self.log.info(db_restart)
        self.assertIn(self.constant.cm_start_failed_msg, db_restart,
                      '重启数据库成功')

        error_msg = f'cat {macro.DB_INSTANCE_PATH}/DBstart.log | grep FATAL'
        self.log.info(error_msg)
        error_res = self.usernode.sh(error_msg).result()
        self.log.info(error_res)
        self.assertIn(f'Fail to realpath config param {self.param}',
                      error_res, '参数修改成功')

    def tearDown(self):
        text = '-----step3:恢复环境为初始值，恢复集群状态; expect:恢复初始值成功，集群状态正常-----'
        self.log.info(text)

        self.log.info('-----恢复参数初始值-----')
        recov_param = self.pri_sh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f"{self.param} = '{self.src}'")
        self.log.info(recov_param)

        self.log.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        self.log.info(restart)

        self.assertTrue(recov_param, '参数修改失败')
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
